package cn.edu.sgu.www.mhxysy.controller.repository;

import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.restful.JsonPage;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.repository.RepositoryPropService;
import cn.edu.sgu.www.mhxysy.vo.prop.PropVO;
import cn.edu.sgu.www.mhxysy.vo.repository.RepositoryPropVO;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "仓库物品管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/repository_prop", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class RepositoryPropController {

    private final RepositoryPropService repositoryPropService;

    @Autowired
    public RepositoryPropController(RepositoryPropService repositoryPropService) {
        this.repositoryPropService = repositoryPropService;
    }

    @ApiOperation("整理仓库")
    @RequestMapping(value = "/arrange", method = RequestMethod.POST)
    public JsonResult<Void> arrange(
            @ApiParam(name = "roleId", value = "角色ID", required = true)
            @NotBlank(message = CustomMessages.ROLE_ID_NOT_NULL) String roleId) {
        repositoryPropService.arrange(roleId);

        return JsonResult.success(CustomMessages.OPERATE_SUCCESS);
    }

    @ApiOperation("通过仓库ID查询仓库道具列表")
    @RequestMapping(value = "/selectByRepositoryId", method = RequestMethod.GET)
    public JsonResult<List<PropVO>> selectByRepositoryId(
            @ApiParam(name = "repositoryId", value = "仓库ID", required = true)
            @NotBlank(message = "仓库" + CustomMessages.ID_NOT_NULL) String repositoryId) {
        List<PropVO> list = repositoryPropService.selectByRepositoryId(repositoryId);

        return JsonResult.success(null, list);
    }

    @ApiOperation("通过道具名称查询仓库道具列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "道具名称", required = true),
            @ApiImplicitParam(name = "roleId", value = "角色ID", required = true)
    })
    @RequestMapping(value = "/selectByName", method = RequestMethod.GET)
    public JsonResult<JsonPage<RepositoryPropVO>> selectByName(
            @NotBlank(message = "道具名称" + CustomMessages.NOT_NULL) String name,
            @NotBlank(message = CustomMessages.ROLE_ID_NOT_NULL) String roleId) {
        List<RepositoryPropVO> list = repositoryPropService.selectByName(name, roleId);

        AtomicInteger total = new AtomicInteger();

        for (RepositoryPropVO repositoryPropVO : list) {
            int size = repositoryPropVO.getList().size();

            total.addAndGet(size);
        }

        return JsonResult.restPage((long) total.get(), list);
    }

}